<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="日期查询" prop="createTime">
        <el-date-picker clearable size="small" v-model="queryParams.createTime" type="date" value-format="yyyy-MM-dd" placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="客户名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入客户名称" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input v-model="queryParams.phone" placeholder="请输入联系电话" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="客户级别" prop="customerRank">
        <el-select v-model="queryParams.customerRank" placeholder="请选择客户级别" clearable size="small">
          <el-option v-for="dict in dict.type.customer_rank" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="客户状态" prop="customerStatus">
        <el-select v-model="queryParams.customerStatus" placeholder="请选择客户状态" clearable size="small">
          <!-- <el-option v-for="dict in dict.type.customer_status" :key="dict.value" :label="dict.label" :value="dict.value" /> -->
          <el-option label="意向客户" value="意向客户"></el-option>
                <el-option label="制样客户" value="制样客户"></el-option>
                <el-option label="成交客户" value="成交客户"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="业务员" prop="userName">
        <el-select v-model="queryParams.userName" placeholder="请选择业务员" filterable clearable>
          <el-option v-for="owner in ownerList" :key="owner.userName" :label="owner.nickName" :value="owner.userName"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <!-- <el-button icon="el-icon-refresh" size="mini" @click="handleExport_">导出</el-button> -->
      </el-form-item>
    </el-form>

    
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport_"
          v-hasPermi="['crm:customer:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="customerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="80" align="center" :show-overflow-tooltip="true" />
      <el-table-column label="客户名称" align="left" prop="name" :show-overflow-tooltip="true" width="240"/>
      <el-table-column label="地区" align="left" prop="region" :show-overflow-tooltip="true" width="240"/>
      <el-table-column label="联系人" align="left" prop="linkman" :show-overflow-tooltip="true" width="200"/>
      <el-table-column label="联系电话" align="left" prop="phone" :show-overflow-tooltip="true" width="200"/>
      <el-table-column label="客户级别" align="left" prop="customerRank" :show-overflow-tooltip="true" width="160"/>
      <el-table-column label="客户行业" align="left" prop="customerIndustry" :show-overflow-tooltip="true" width="160"/>
      <el-table-column label="最新跟进记录" align="left" prop="lastUpdateRecord" :show-overflow-tooltip="true" width="200" />
      <el-table-column label="负责人" align="left" prop="owner" :show-overflow-tooltip="true"  width="160"/>
      <el-table-column label="创建时间" align="left" prop="createTime" width="220" :show-overflow-tooltip="true" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
  </div>
</template>

<script>
import {
  listDealCustomer,
} from '@/api/crm/customer'
import {
  listOwner
} from '@/api/crm/owner'
import axios from "axios";

export default {
  name: 'Customer',
  dicts: [
    'clues_source',
    'customer_industry',
    'clues_status',
    'customer_rank',
    'deal_status',
    'customer_status',
  ],
  data () {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 是否勾选
      selected: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户表格数据
      customerList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      //业务员数组默认值
      ownerList:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        name: null,
        linkman: null,
        phone: null,
        region: null,
        customerIndustry: null,
        customerRank: null,
        customerStatus: null,
        cluesName: null,
        cluesSource: null,
        cluesStatus: null,
        userName: null,
        
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: '客户名称不能为空', trigger: 'blur' },
        ],
        linkman: [
          { required: true, message: '联系人不能为空', trigger: 'blur' },
        ],
        phone: [
          { required: true, message: '联系电话不能为空', trigger: 'blur' },
        ],
      },
      transferCustomer: false,
      toPool: false,
    }
  },
  created () {
    this.getList();
    this.getOwnerList();
  },
  methods: {

      /** 导出按钮操作 */
   handleExport_(event) {
      event.preventDefault();
      this.handleExport();
  },

  async handleExport() {

const Authorization = 'Bearer' + 
(localStorage.getItem('Token')||'eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJmQm9wV2otVTFIYjlTa0ZHMHZYQ1lRVVhkRkdDMGhXRTBkVjJCN1FaSl9ZIn0.eyJleHAiOjE3NTExNzk5NzMsImlhdCI6MTc0NTk5NTk3MywianRpIjoiOGQ5YWEyY2MtMTI4Mi00OGZmLTg5ZjctNTY3ZGZiMjRkYWQyIiwiaXNzIjoiaHR0cDovLzE3Mi4xNi4wLjM6ODQxMS9hdXRoL3JlYWxtcy9lZGdlIiwiYXVkIjpbIm5vY29jb25zb2xlIiwiYWNjb3VudCIsIm9jcy1jb250cm9sbGVyIl0sInN1YiI6ImIwMjZiYzUxLTEzMjItNDJiOC1iZTg0LWU5MDJiNmFjYmM3YSIsInR5cCI6IkJlYXJlciIsImF6cCI6Imppbmd6aGkiLCJzZXNzaW9uX3N0YXRlIjoiMzgyZmFiNTktYWI0ZC00ZTZjLWJkNWMtNTNlZDIzYzM0ZDUwIiwiYWNyIjoiMSIsInJlYWxtX2FjY2VzcyI6eyJyb2xlcyI6WyLnsr7liLYt57O757uf566h55CG5ZGYIiw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jy-crm-app/crm/customer/export2', 
method: 'post', 
responseType: 'blob',
params: { dealStatus: 1 },
headers: {
Authorization
}
}).then(response => {
// 请求成功，处理响应数据
const url = window.URL.createObjectURL(new Blob([response.data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' })); // 创建一个Blob对象，并生成一个URL
const link = document.createElement('a'); // 创建一个临时的<a>标签用于下载
link.href = url; // 设置<a>标签的href属性为Blob对象的URL
link.setAttribute('download', '成交客户.xlsx'); // 设置下载文件名，扩展名根据实际文件类型设置
document.body.appendChild(link); // 将<a>标签添加到页面中，以便可以点击
link.click(); // 模拟点击<a>标签，触发下载
document.body.removeChild(link); // 下载触发后，从页面中移除<a>标签
window.URL.revokeObjectURL(url); // 释放创建的URL对象，避免内存泄漏
}).catch(error => {
// 请求失败，处理错误
console.error('下载文件失败:', error);
alert('下载文件失败，请稍后重试或联系管理员');
});

},
    /** 查询成交客户列表 */
    getList () {
      this.loading = true
      listDealCustomer(this.queryParams).then((response) => {
        this.customerList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    getOwnerList () {
      this.loading = true
      listOwner(this.queryParams).then((response) => {
        this.ownerList = response.data
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map((item) => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
      this.selected = selection.length
    },

    /** 导出按钮操作 */
    // handleExport () {
    //   this.download(
    //     'crm/customer/export',
    //     {
    //       ...this.queryParams,
    //     },
    //     `customer_${new Date().getTime()}.xlsx`
    //   )
    // },
  },
}
</script>
